<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商铺名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地区" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入地区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="销量" prop="sold">
        <el-input
          v-model="queryParams.sold"
          placeholder="请输入销量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态">
          <el-option label="全部" value="" />
          <el-option label="启用" value="0" />
          <el-option label="禁用" value="1" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:shop:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:shop:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:shop:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:shop:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shopList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="商铺名称" align="center" prop="name" />
      <el-table-column label="商铺类型" align="center" prop="typeName" />
      <el-table-column label="图片" align="center" prop="images" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.images" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="地区" align="center" prop="area" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="销量" align="center" prop="sold" />
      <el-table-column label="评论数量" align="center" prop="comments" />
      <el-table-column label="评分" align="center" prop="score" />
      <el-table-column label="营业时间" align="center" prop="openHours" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === '0' ? 'success' : 'danger'">
            {{ scope.row.status === '0' ? '启用' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:shop:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:shop:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-comments"
            @click="handleOpen(scope.row)"
          >查看详细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商铺信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商铺名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入商铺名称" />
        </el-form-item>
        <el-form-item label="商铺类型" prop="typeId">
          <el-select v-model="form.typeId" placeholder="请选择商铺类型" clearable>
            <el-option v-for="type in shopTypes" :key="type.value" :label="type.label" :value="type.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="图片" prop="images">
          <image-upload v-model="form.images"/>
        </el-form-item>
        <el-form-item label="地区" prop="area">
          <el-input v-model="form.area" placeholder="请输入地区" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="营业时间" prop="openHours">
          <el-input v-model="form.openHours" placeholder="请输入营业时间，例如 10:00-22:00" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="0">启用</el-radio>
            <el-radio :label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listShop, getShop, delShop, addShop, updateShop, changeShopStatus } from "@/api/system/shop"

export default {
  name: "Shop",
  data() {
    return {
      shopTypes: [
        { label: '美食', value: 1 },
        { label: 'KTV', value: 2 },
        { label: '丽人·美发', value: 3 },
        { label: '健身运动', value: 4 },
        { label: '按摩·足疗', value: 5 },
        { label: '美容SPA', value: 6 },
        { label: '亲子游乐', value: 7 },
        { label: '酒吧', value: 8 },
        { label: '轰趴馆', value: 9 },
        { label: '美睫·美甲', value: 10 }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商铺信息表格数据
      shopList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        typeId: null,
        images: null,
        area: null,
        address: null,
        avgPrice: null,
        sold: null,
        comments: null,
        score: null,
        openHours: null,
        status: null,
        isDeleted: null
      },
      // 表单参数
      form: {
        status: 0 // 默认启用
      },
      // 状态映射
      statusMap: {
        0: '启用',
        1: '禁用'
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "商铺名称不能为空", trigger: "blur" }
        ],
        typeName: [
          { required: true, message: "商铺类型不能为空", trigger: "blur" }
        ],
        images: [
          { required: true, message: "商铺图片，多个图片以','隔开不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "地址不能为空", trigger: "blur" }
        ],
        openHours: [
          { required: true, message: "营业时间不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 状态格式化
    statusFormat(status) {
      return this.statusMap[status] || '未知'
    },

    /** 查询商铺信息列表 */
    getList() {
      this.loading = true
      listShop(this.queryParams).then(response => {
        this.shopList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        typeId: null,
        typeName: null,
        images: null,
        area: null,
        address: null,
        avgPrice: null,
        sold: null,
        comments: null,
        score: null,
        openHours: null,
        createBy: null,
        updateBy: null,
        createTime: null,
        updateTime: null,
        status: 0, // 默认启用
        isDeleted: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      // 直接重置查询参数对象
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        name: null,
        typeId: null,
        typeName: null,
        images: null,
        area: null,
        address: null,
        avgPrice: null,
        sold: null,
        comments: null,
        score: null,
        openHours: null,
        status: null,
        isDeleted: null
      }
      this.handleQuery()
    },
    handleOpen(row){
      this.$router.push('/system/shop/comments/index/' +  row.id);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加商铺信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getShop(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改商铺信息"
      })
    },
    /** 提交按钮 */
    submitForm() {
      //将form中的images字段的双引号替换为单引号
      const params = this.form;
      params.images = params.images.replaceAll('"', "'");
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateShop(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addShop(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除商铺信息编号为"' + ids + '"的数据项？').then(() => {
        return delShop(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 状态切换操作 */
    toggleStatus(row) {
      const newStatus = row.status === 0 ? 1 : 0
      const message = newStatus === 0 ? '启用' : '禁用'

      this.$modal.confirm(`是否确认${message}该商铺？`).then(() => {
        return changeShopStatus(row.id, newStatus)
      }).then(() => {
        row.status = newStatus
        this.$modal.msgSuccess(`${message}成功`)
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/shop/export', {
        ...this.queryParams
      }, `shop_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
